package com.g2room.driving.grap.life;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.g2room.driving.grap.GenericGrap;
import com.g2room.driving.grap.life.model.po.LifeCelebrity;
import com.g2room.driving.grap.life.model.po.LifeDistrict;
import com.g2room.driving.grap.life.model.po.LifeFood;
import com.g2room.driving.grap.life.model.po.LifeScenery;
import com.g2room.driving.util.ImageUtil;
import com.g2room.myuall.grap.ky.model.po.GrapErr;
import com.g2room.myuall.grap.univ.po.LifeNews;
import com.g2room.myuall.util.DbUtil;
import com.g2room.myuall.util.HtmlUtil;

public class GrapAraeData extends GenericGrapNews {
	// http://ren.bytravel.cn/Celebrity/index130.html
	public static String table = "life_food";
	public String baseUrl = "http://www.tcmap.com.cn";
	public String peopleBaseUrl = "http://mren.bytravel.cn";
	private String source = "博雅人名网";

	private static  List<String> detailUrls = new ArrayList<String>();
	public static void main(String[] args) throws Exception{
		getSourceUrl(table);
		GrapAraeData gkd = new GrapAraeData();
//		gkd.getArea("http://www.tcmap.com.cn/chongqing/");
//		gkd.getPeplo();
//		gkd.getFoods();
//		gkd.getCelebritys();
		gkd.getMingsues();
    }
	// http://www.cqlsmrw.com/mrjj/
	
	public void getMingsues() {
		List<String> listUrls = new ArrayList<String>();
		listUrls.add("http://www.uu456.com/d13869.htm");
		listUrls.add("http://www.uu456.com/d13845.htm");
		listUrls.add("http://www.uu456.com/d13878.htm");
		listUrls.add("http://www.uu456.com/d13847.htm");
		listUrls.add("http://www.uu456.com/d13876.htm");
		listUrls.add("http://www.uu456.com/d13858.htm");
		listUrls.add("http://www.uu456.com/d13871.htm");
		listUrls.add("http://www.uu456.com/d13864.htm");
		listUrls.add("http://www.uu456.com/d13861.htm");
		listUrls.add("http://www.uu456.com/d13849.htm");
		listUrls.add("http://www.uu456.com/d13880.htm");
		listUrls.add("http://www.uu456.com/d13868.htm");
		listUrls.add("http://www.uu456.com/d13844.htm");
		listUrls.add("http://www.uu456.com/d13875.htm");
		listUrls.add("http://www.uu456.com/d13857.htm");
		listUrls.add("http://www.uu456.com/d13873.htm");
		listUrls.add("http://www.uu456.com/d13851.htm");
		listUrls.add("http://www.uu456.com/d13841.htm");
		listUrls.add("http://www.uu456.com/d13853.htm");
		listUrls.add("http://www.uu456.com/d13874.htm");
		listUrls.add("http://www.uu456.com/d13859.htm");
		listUrls.add("http://www.uu456.com/d13865.htm");
		listUrls.add("http://www.uu456.com/d13846.htm");
		listUrls.add("http://www.uu456.com/d13872.htm");
		listUrls.add("http://www.uu456.com/d13854.htm");
		listUrls.add("http://www.uu456.com/d13856.htm");
		listUrls.add("http://www.uu456.com/d13842.htm");
		listUrls.add("http://www.uu456.com/d13867.htm");
		listUrls.add("http://www.uu456.com/d13848.htm");
		listUrls.add("http://www.uu456.com/d13850.htm");
		listUrls.add("http://www.uu456.com/d13860.htm");
		listUrls.add("http://www.uu456.com/d13877.htm");
		listUrls.add("http://www.uu456.com/d13852.htm");
		listUrls.add("http://www.uu456.com/d13866.htm");
		listUrls.add("http://www.uu456.com/d13863.htm");
		listUrls.add("http://www.uu456.com/d13855.htm");
		listUrls.add("http://www.uu456.com/d13843.htm");
		listUrls.add("http://www.uu456.com/d13879.htm");
		listUrls.add("http://www.uu456.com/d13870.htm");
		listUrls.add("http://www.uu456.com/d13862.htm");
		
		List<LifeNews> lces = new ArrayList<LifeNews>();
		for(String url : listUrls) {
			LifeNews lc = getMingshu(url);
			if(lc != null) lces.add(lc);
		}
		if(lces.size() > 0) insert2LifeArtiDb(lces);
	}
	
	public LifeNews getMingshu(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent("名人详情");
			ge.setDescription("获取名人详情错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		if(null == doc) return null;
		LifeNews lc = new LifeNews();
		lc.setSource("优游旅游网");
		lc.setSourceUrl(url);
		lc.setTypeId(2);
		lc.setTypeName("民俗风情");
		
		Elements els = doc.select("title");
		if(null != els && !els.isEmpty()) {
			lc.setTitle(els.first().text().replace(" 重庆 旅游大全 民风民俗", ""));
		}
		
		els = doc.select("body>div>div>table>tbody>tr>td>div>div>table>tbody>tr>td>p");
		if(null != els && !els.isEmpty()) {
			StringBuffer sb = new StringBuffer();
			for(Element el : els) {
				if(el.text().length() < 1 || el.text().contains("优游旅游网（") || (null != el.attr("style") && el.attr("style").equalsIgnoreCase("background:#F7F7FF;padding:10px;font-size: 11pt;COLOR:#0066CC "))) continue;
				sb.append(el.outerHtml().replaceAll("　", "").replaceAll("&nbsp;", ""));
			}
			lc.setContent(sb.toString());
			String c = HtmlUtil.delAllTag(sb.toString());
			lc.setSummary(c.substring(0, Math.min(200, c.length())));
		}
		return lc;
	}
	
	//////////////////////////////
	public void getCelebritys() {
		for(int i = 20; i < 21; i++) {
			String url = "http://www.cqlsmrw.com/mrjj/product.php?lang=cn&class1=122&page="+i;
			Document doc = null;
			try {
				doc = GenericGrap.getDocByIp(url);
			} catch(Exception e) {
				GrapErr ge = new  GrapErr();
				ge.setUrl(url);
				ge.setType("1");
				ge.setContent("名人");
				ge.setDescription("获取名人 错误："+e.getMessage());
				insert2ErrDb(ge);
			}
			Elements els = doc.select("ul.list_1 li a");
			
			List<LifeCelebrity> ces = new ArrayList<LifeCelebrity>();
			if(null != els && !els.isEmpty()) {
				for(Element el : els) {
					String href = "http://www.cqlsmrw.com/mrjj/" + el.attr("href");
					if(soruceUrls.contains(href)) continue;
					LifeCelebrity lc = new LifeCelebrity();
					lc.setSourceUrl(href);
					String title = el.attr("title");
					// 截取生日和忌日 陈纳德（1890——1958）美国人
					if(title.contains("（")) {
						String[] str = title.split("（");
						System.out.println(title);
						lc.setName(str[0]);
						String[] str1 = str[1].split("）");
						if(str1.length == 1) {
							str1 =  str[1].split("\\)");
						}
						lc.setCityName(str[1]);
						
						if(title.contains("——")) {
							str1 = str1[0].split("——");
							lc.setBirthday(str1[0]);
							lc.setDeathday(str1[1]);
						}
					} else {
						lc.setName(title);
					}
					// 封面
					Elements tels = el.select("img");
					if(null != tels && !tels.isEmpty()) {
						// include/thumb.php?dir=../upload/201602/1456312669.jpg&x=156&y=185
						// include/thumb.php?dir=../upload/201602/1456312669.jpg&x=156&y=185
						lc.setCover(tels.first().attr("src").replaceFirst("../", "http://www.cqlsmrw.com/"));
					}
					ces.add(lc);
					soruceUrls.add(href);
				}
			}
			
			List<LifeCelebrity> lces = new ArrayList<LifeCelebrity>();
			for(LifeCelebrity c : ces) {
				LifeCelebrity lc = getCelebrity(c);
				if(lc != null) lces.add(lc);
			}
			if(lces.size() > 0) insertIntoLcDb(lces);
		}
	}
	
	public LifeCelebrity getCelebrity(LifeCelebrity lc) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocByIp(lc.getSourceUrl());
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(lc.getSourceUrl());
			ge.setType("2");
			ge.setContent("名人详情");
			ge.setDescription("获取名人详情错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		if(null == doc) return null;
		
		lc.setSource("重庆历史名人馆");
		
		Elements els = doc.select("#metbox div.box div div");
		if(null != els && !els.isEmpty()) {
			lc.setDescription(els.first().html());
		}
		
		els = doc.select("#metbox div.box div div img");
		if(null != els && !els.isEmpty()) {
			try {
				ImageUtil.downloadImage(els.first().attr("src"), "mr");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			lc.setCover(els.first().attr("src"));
		}
		return lc;
	}
	
	//////////////////////////////
	
	public void getFoods() {
		for(int i = 0; i < 2; i++) {
			String url = "http://m.bytravel.cn/produce/ms/index130_list"+(i>0?i:"")+".html";
			Document doc = null;
			try {
				doc = GenericGrap.getDoc(url, "gb2312");
			} catch(Exception e) {
				GrapErr ge = new  GrapErr();
				ge.setUrl(url);
				ge.setType("1");
				ge.setContent("名人");
				ge.setDescription("获取名人 错误："+e.getMessage());
				insert2ErrDb(ge);
			}
			Elements els = doc.select("table ul#titlename li a");

			List<String> urls = new ArrayList<String>();
			if(null != els && !els.isEmpty()) {
				for(Element el : els) {
					String href = "http://m.bytravel.cn" + el.attr("href");
					if(soruceUrls.contains(href)) continue;
					urls.add(href);
					soruceUrls.add(href);
				}
			}

			List<LifeFood> lces = new ArrayList<LifeFood>();
			for(String u : urls) {
				LifeFood lc = getFood(u);
				if(lc != null) lces.add(lc);
			}
			if(lces.size() > 0) insert2LsfDb(lces);
		}
	}
	
	public LifeFood getFood(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDoc(url, "gb2312");
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent("名人详情");
			ge.setDescription("获取名人详情错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		if(null == doc) return null;
		
		LifeFood lc = new LifeFood();
		lc.setSource(source);
		lc.setSourceUrl(url);
		Elements els = doc.select("body>div#mainbao a");
		if(null != els && !els.isEmpty()) {
			lc.setCityName(els.get(els.size() - 1).text());
		}
		
		els = doc.select("body>div");
		if(null != els && !els.isEmpty() && !url.equals("http://m.bytravel.cn/produce3/shicaoshangluobo.html")) {
			for(Element el : els) {
				String style = el.attr("align");
				if(null != style && style.equals("center")) {
					lc.setName(el.text());
					break;
				}
			}
		}
		
		els = doc.select("body>header>div>a>img");
		if(null != els && !els.isEmpty()) {
			lc.setCover(els.first().attr("src"));
		}

		els = doc.select("body div");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				String style = el.attr("style");
				if(null != style && style.equals("margin:5px 15px 5px 15px;clear:both")) {
					el.select("div").remove();
//					el.select("strong").remove();
//					Elements tels = el.select("strong");
//					if(tels != null && !tels.isEmpty()) {
//						tels.first().remove();
//					}
					lc.setDescription(getUnUnicStr(HtmlUtil.delATag(el.html()).replaceAll("&emps;", "").replaceAll("　", "")));
//					lc.setSummary(el.text().replaceAll("&emps;", "").replaceAll("　", ""));
//					lc.setSummary(lc.getSummary().substring(0, Math.min(200, lc.getSummary().length())));
					break;
				}
			}
		}
		lc.setKindName("民俗文化");
		return lc;
	}
	
	/////////////////////////////////////
	
	public void getPeplo() {
		for(int i = 0; i < 20; i++) {
			String url = "http://mren.bytravel.cn/Celebrity/index130_list"+(i>0?i:"")+".html";
			Document doc = null;
			try {
				doc = GenericGrap.getDoc(url, "gb2312");
			} catch(Exception e) {
				GrapErr ge = new  GrapErr();
				ge.setUrl(url);
				ge.setType("1");
				ge.setContent("名人");
				ge.setDescription("获取名人 错误："+e.getMessage());
				insert2ErrDb(ge);
			}
			Elements els = doc.select("table ul#titlename li a");

			List<String> urls = new ArrayList<String>();
			if(null != els && !els.isEmpty()) {
				for(Element el : els) {
					String href = peopleBaseUrl + el.attr("href");
					if(soruceUrls.contains(href)) continue;
					urls.add(href);
					soruceUrls.add(href);
				}
			}

			List<LifeCelebrity> lces = new ArrayList<LifeCelebrity>();
			for(String u : urls) {
				LifeCelebrity lc = getFamous(u);
				if(lc != null) lces.add(lc);
			}
			if(lces.size() > 0) insertIntoLcDb(lces);
		}
	}
	
	public LifeCelebrity getFamous(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDoc(url, "gb2312");
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent("名人详情");
			ge.setDescription("获取名人详情错误："+e.getMessage());
			insert2ErrDb(ge);
			return null;
		}
		if(null == doc) return null;
		
		LifeCelebrity lc = new LifeCelebrity();
		lc.setSource(source);
		lc.setSourceUrl(url);
		Elements els = doc.select("#mainbao a");
		if(null != els && !els.isEmpty()) {
			lc.setCityName(els.get(els.size() - 1).text());
		}
		
		els = doc.select("body>header>div");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				String style = el.attr("align");
				if(null != style && style.equals("center")) {
					lc.setName(el.text());
					break;
				}
			}
		}
		
		els = doc.select("body div");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				String style = el.attr("style");
				if(null != style && style.equals("margin:0 5px 20px 5px;clear:both")) {
//					els = el.children();
					el.select("div").remove();
					el.select("strong").remove();
//					Elements tels = el.select("strong");
//					if(tels != null && !tels.isEmpty()) {
//						tels.first().remove();
//					}
					lc.setDescription(HtmlUtil.delATag(el.html()).replaceAll("&emps;", "").replaceAll("　", ""));
					lc.setSummary(el.text().replaceAll("&emps;", "").replaceAll("　", ""));
					lc.setSummary(lc.getSummary().substring(0, Math.min(200, lc.getSummary().length())));
					break;
				}
			}
		}

		els = doc.select("body div");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				if(null == el.text() || !el.text().contains("[")) continue;
				
				int num = el.text().split("\\[").length;
				if(num < 0) continue;
				
				if(num == 3) {
					String[] strs = el.text().replaceAll("\\]", "").split("\\[");
					lc.setDynasty(strs[1]);
					// [公元1872年－1908年，辛亥革命烈士]  [明末武将] [？－1908年]
					if(strs[2].contains("，") && strs[2].contains("－")) {
						String[] strs1 = strs[2].split("，");
						lc.setBirthday(strs1[0].split("－")[0].replace("公元", ""));
						lc.setDeathday(strs1[0].split("－")[1]);
						lc.setTitle(strs1[1]);
					} else if(strs[1].contains("－")) {
						lc.setBirthday(strs[1].split("－")[0].replace("公元", ""));
						lc.setDeathday(strs[1].split("－")[1]);
					} else {
						lc.setTitle(strs[1]);
					}
					break;
				} else if(num == 2) {
					String strs = el.text().replaceAll("\\]", "").replaceAll("\\[", "");
//						lc.setDynasty(strs[0]);
					// [公元1872年－1908年，辛亥革命烈士]  [明末武将] [？－1908年]
					if(strs.contains("，") && strs.contains("－")) {
						String[] strs1 =strs.split("，");
						lc.setBirthday(strs1[0].split("－")[0].replace("公元", ""));
						lc.setDeathday(strs1[0].split("－")[1]);
						lc.setTitle(strs1[1]);
					} else if(strs.contains("－")) {
						lc.setBirthday(strs.split("－")[0].replace("公元", ""));
						lc.setDeathday(strs.split("－")[1]);
					} else {
						lc.setTitle(strs);
					}
					break;
				}
			}
		}
		
		return lc;
	}
	
	public void getArea(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDocumentByIp(url);
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("1");
			ge.setContent("区域");
			ge.setDescription("获取Area 错误："+e.getMessage());
			insert2ErrDb(ge);
		}
		Elements els = doc.select("tr td strong a");
		List<String> urls = new ArrayList<String>();
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				urls.add(baseUrl + el.attr("href"));
			}
		}
		for(String u : urls) getDistrict(u);
	}
	

	public void getDistrict(String url) {
		Document doc = null;
		try {
			doc = GenericGrap.getDoc(url, "GB2312");
		} catch(Exception e) {
			GrapErr ge = new  GrapErr();
			ge.setUrl(url);
			ge.setType("2");
			ge.setContent("区域2");
			ge.setDescription("获取District 错误："+e.getMessage());
			insert2ErrDb(ge);
		}
		LifeDistrict ld = new LifeDistrict();
		Elements els = doc.select("table[width=396px] td");
		if(null != els && !els.isEmpty()) {
			String text;
			for(Element el : els) {
				text = el.text();
				if(text.contains("地名:")) {
					ld.setName(text.trim().replace("地名:", ""));
					continue;
				} else if(text.contains("隶属:")) {
					ld.setBelongToName(text.trim().replace("隶属:", ""));
					continue;
				} else if(text.contains("行政代码:")) {
					ld.setCode(text.trim().replace("行政代码:", ""));
					continue;
				} else if(text.contains("长途区号:")) {
					ld.setAreaCode(text.trim().replace("长途区号:", ""));
					continue;
				} else if(text.contains("邮政编码:")) {
					ld.setPostcode(text.trim().replace("邮政编码:", ""));
					continue;
				} else if(text.contains("车牌号码:")) {
					ld.setPlateNumber(text.trim().replace("车牌号码:", ""));
					continue;
				} else if(text.contains("行政级别:")) {
					ld.setLevel(text.trim().replace("行政级别:", ""));
					continue;
				} else if(text.contains("人口数量:")) {
					ld.setPopulation(text.trim().replace("人口数量:", ""));
					continue;
				} else if(text.contains("人口密度:")) {
					ld.setPopulationDensity(text.trim().replace("人口密度:", ""));
					continue;
				} else if(text.contains("辖区面积:")) {
					ld.setArea(text.trim().replace("辖区面积:", ""));
					continue;
				}
			}
		}
		// 获取介绍
		els = doc.select(".f14");
		if(null != els && !els.isEmpty()) {
			boolean flag = false;
			for(Element el : els.first().children()) {
				if(el.tagName().equals("br") && null != el.attr("clear") && el.attr("clear").equals("all")) {
					flag = true;
				}
				if(flag) el.remove();
			}
			ld.setDescription(HtmlUtil.delAllTag(els.first().html()));
			String summary = els.first().text();
			if(StringUtil.isNotBlank(summary)) {
				ld.setSummary(summary.substring(0, Math.min(150, summary.length())).replaceAll("　", ""));
			}
		}
		ld.setSource(source);
		ld.setSourceUrl(url);
		insert2LdDb(ld);
		// 获取下级列表
		els = doc.select("table[width=738] td strong a");
		if(null != els && !els.isEmpty()) {
			for(Element el : els) {
				getDistrict(baseUrl + el.attr("href"));
			}
		}
	}
	
	
	public void insert2LifeArtiDb(List<LifeNews> lns) {
		String sql = "insert into life_arti(content,title,summary,type_id,type_name,source,source_url) values(?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn("life_grap").prepareStatement(sql);
			for(LifeNews ln : lns) {
				ps.setString(1, getStr(ln.getContent()));
				ps.setString(2, getStr(ln.getTitle()));
				ps.setString(3, getStr(ln.getSummary()));
				ps.setInt(4, ln.getTypeId() == null ? 0 : ln.getTypeId());
				ps.setString(5, getStr(ln.getTypeName()));
				ps.setString(6, getStr(ln.getSource()));
				ps.setString(7, getStr(ln.getSourceUrl()));
				ps.addBatch();
			}
			ps.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new GrapErr();
			ge.setType("5");
			ge.setContent(JSON.toJSONString(lns));
			ge.setDescription("insert DB 错误：" + e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if (null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
				}
			}
		}
	}
	
	private String getStr(String str) {
		try {
			str = null == str ? null : str.replaceAll("[\\ud83c\\udc00-\\ud83c\\udfff]|[\\ud83d\\udc00-\\ud83d\\udfff]|[\\u2600-\\u27ff]", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public void insert2LsfDb(List<LifeFood> lfs) {
		String sql = "insert into life_food(source_url,kind_name,cover,province_name,images,name,description,city_name,source) values(?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn(db).prepareStatement(sql);
			for(LifeFood cc : lfs) {
				ps.setString(1, cc.getSourceUrl());
				ps.setString(2, cc.getKindName());
				ps.setString(3, cc.getCover());
				ps.setString(4, cc.getProvinceName());
				ps.setString(5, cc.getImages());
				ps.setString(6, cc.getName());
				ps.setString(7, cc.getDescription());
				ps.setString(8, cc.getCityName());
				ps.setString(9, cc.getSource());
				ps.addBatch();
			}
			ps.executeBatch();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lfs));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void insertIntoLcDb(List<LifeCelebrity> lcs) {
		String sql = "insert into life_celebrity(deathday,source_url,description,name,city_name,summary,title,birthday,source,dynasty, cover) values(?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn(db).prepareStatement(sql);
			for(LifeCelebrity cc : lcs) {
				ps.setString(1, cc.getDeathday());
				ps.setString(2, cc.getSourceUrl());
				ps.setString(3, cc.getDescription());
				ps.setString(4, cc.getName());
				ps.setString(5, cc.getCityName());
				ps.setString(6, cc.getSummary());
				ps.setString(7, cc.getTitle());
				ps.setString(8, cc.getBirthday());
				ps.setString(9, cc.getSource());
				ps.setString(10, cc.getDynasty());
				ps.setString(11, cc.getCover());
				ps.addBatch();	
			}
			ps.executeBatch();
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(lcs));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void insert2LdDb(LifeDistrict ld) {
		String sql = "insert into life_district(source_url,belong_to_name,area,description,area_code,level,population,plate_number,code,name,summary,postcode,source,population_density) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		PreparedStatement ps = null;
		try {
			ps = DbUtil.getConn("life_grap").prepareStatement(sql);
			
			ps.setString(1, ld.getSourceUrl());
			ps.setString(2, ld.getBelongToName());
			ps.setString(3, ld.getArea());
			ps.setString(4, ld.getDescription());
			ps.setString(5, ld.getAreaCode());
			ps.setString(6, ld.getLevel());
			ps.setString(7, ld.getPopulation());
			ps.setString(8, ld.getPlateNumber());
			ps.setString(9, ld.getCode());
			ps.setString(10, ld.getName());
			ps.setString(11, ld.getSummary());
			ps.setString(12, ld.getPostcode());
			ps.setString(13, ld.getSource());
			ps.setString(14, ld.getPopulationDensity());
			ps.execute();	
		} catch (Exception e) {
			e.printStackTrace();
			GrapErr ge = new  GrapErr();
			ge.setType("6");
			ge.setContent(JSON.toJSONString(ld));
			ge.setDescription("插入Detail错误："+e.getMessage());
			insert2ErrDb(ge);
			return;
		} finally {
			if(null != ps) {
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	

	public void insert2ErrDb(GrapErr ge) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getConn("life_grap").prepareStatement("insert into grap_err(description,url,content,type) values(?,?,?,?)");
			grapPs.setString(1, ge.getDescription());
			grapPs.setString(2, ge.getUrl());
			grapPs.setString(3, ge.getContent());
			grapPs.setString(4, ge.getType());
			grapPs.execute();
			grapPs.close();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static List<GrapErr> getErrs() {
		List<GrapErr> univs = new ArrayList<GrapErr>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select * from grap_err";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				GrapErr univ = new GrapErr();
				univ.setId(rs.getInt("id"));
				univ.setUrl(rs.getString("url"));
				univ.setType(rs.getString("type"));
				univ.setContent(rs.getString("content"));
				univs.add(univ);
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return univs;
	}
	
	public static void delErr(int id) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("delete from grap_err where id=?");
			grapPs.setInt(1, id);
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static void updateLs(LifeScenery ls) {
		PreparedStatement grapPs = null;
		try {
			grapPs = DbUtil.getComConn().prepareStatement("update life_scenery set cost =?,interest =?,scenery =?  where id=?");
			grapPs.setString(1, ls.getCost());
			grapPs.setString(2, ls.getInterest());
			grapPs.setString(3, ls.getScenery());
			grapPs.setInt(4, ls.getId());
			grapPs.execute();
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if(null != grapPs) {
				try {
					grapPs.close();
				} catch (SQLException e) {
					
				}
			}
		}
	}
	
	public static Map<Integer, String> getSensList() {
		Map<Integer, String> map =new HashMap<Integer, String>();
		try {
			Connection connection = DbUtil.getComConn();
			Statement statement = connection.createStatement();
			String sql = "select  id,source_url from life_scenery";
			ResultSet rs = statement.executeQuery(sql);
			while(rs.next()) {
				map.put(rs.getInt("id") ,rs.getString("source_url"));
			}
			rs.close();
			statement.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
		return map;
	}

}
